import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:sui_shou_pai/Utils/storage_util.dart';
import 'package:sui_shou_pai/camera/model/department.dart';
import 'package:sui_shou_pai/module/color.dart';
import 'package:sui_shou_pai/module/effect.dart';
import 'package:sui_shou_pai/network/http_request.dart';

class AlertPersonPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return AlertPersonState();
  }
}

class AlertPersonState extends State<AlertPersonPage> {
  List<Department> departments = [];
  Department choose;
  String chooseWord = "";

  Future<void> initData() async {
    HttpRequest.request("/department/getDepartmentPerson",
            method: "get", token: await StorageUtil.getStringItem("token"))
        .then((value) {
      if (value.data["code"] == 200) {
        List<dynamic> result = value.data["result"];
        List<Department> depart = [];
        for (int i = 0; i < result.length; i++) {
          Department department = Department.formMap(result[i]);
          depart.add(department);
        }
        setState(() {
          departments = depart;
        });
      } else {
        Fluttertoast.showToast(msg: "联系人加载失败");
      }
    });
  }

  @override
  void initState() {
    super.initState();
    initData();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("提醒谁看"),
        centerTitle: true,
        actions: [
          IconButton(
              onPressed: () {
                Navigator.pop(context, choose);
              },
              icon: Icon(
                Icons.check_outlined,
                size: 25,
              ))
        ],
      ),
      body: Container(
          child: ListView(
        children: [
          Padding(
            child: Row(
              children: [
                Icon(Icons.person_add_outlined,
                    size: 25,
                    color: chooseWord == "" ? greyCCCCCC : Colors.black),
                Expanded(
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Container(
                          margin: EdgeInsets.only(left: 15),
                          child: chooseWord == ""
                              ? Text("选择负责人",
                                  style: TextStyle(
                                      fontSize: 20, color: greyCCCCCC))
                              : Text("$chooseWord",
                                  style: TextStyle(
                                      fontSize: 20, color: Colors.black))),
                      chooseWord == ""
                          ? Text("")
                          : Padding(
                              padding: EdgeInsets.only(right: 6),
                              child: IconButton(
                                onPressed: () {
                                  setState(() {
                                    chooseWord = "";
                                    choose = null;
                                    //print(choose_word);
                                  });
                                },
                                icon: Icon(Icons.clear, size: 25),
                              )),
                    ],
                  ),
                )
              ],
            ),
            padding: EdgeInsets.only(top: 22, left: 25, bottom: 22),
          ),
          Divider(height: 1.0, color: greyCCCCCC),
          Container(
            height: MediaQuery.of(context).size.height - 166,
            padding: EdgeInsets.only(left: 30),
            child: ListView.builder(
              itemCount: departments.length,
              itemBuilder: (context, index) {
                return createItem(index);
              },
            ),
          )
        ],
      )),
    );
  }

  Widget createItem(int index) {
    return Column(
      children: [
        GestureDetector(
          behavior: HitTestBehavior.opaque,
          child: Container(
            margin: EdgeInsets.only(top: 15, bottom: 15),
            height: 50,
            child: Row(
              children: [
                Container(
                  child: ClipRRect(
                    borderRadius: borderRadiusAll40(),
                    child: Image.network(departments[index].employee.avatar,
                        fit: BoxFit.cover),
                  ),
                  width: 50.0,
                  height: 50.0,
                ),
                Padding(
                    padding: EdgeInsets.only(left: 20),
                    child: Text(
                        "${departments[index].name}：   ${departments[index].employee.name}",
                        style: TextStyle(fontSize: 20)))
              ],
            ),
          ),
          onTap: () {
            setState(() {
              choose = departments[index];
              chooseWord =
                  "${departments[index].name}：   ${departments[index].employee.name}";
              //print(choose.employee.name);
            });
          },
        ),
        Divider(height: 1.0, indent: 50, color: greyCCCCCC),
      ],
    );
  }
}
